﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebSite_that_use_Wizard.NET
{
	public partial class _Default2 : System.Web.UI.Page, ICallbackEventHandler
	{
		private Wizard.Asp.AspEngine mWizardEngine;
        private Wizard.Source.ISession mWizardSession;
        private Wizard.IDataStoreContext mDataStoreContext;

        private String hint;
        public String mGetHintFromServer;
        public String mGetHint2FromServer;

        //Define GetCallbackEventReference method  //whose signature is as the following:
        //public string GetCallbackEventReference(
        //Control target,
        //string argument,     
        //string clientCallback,
        //string context,
        //string clientErrorCallback,
        //bool useAsync)
        void Page_Load(object sender, System.EventArgs e)
        {
            mGetHintFromServer = this.ClientScript.GetCallbackEventReference
              (this, "message", "ShowHint", "null", "null", false);

            mGetHint2FromServer = this.ClientScript.GetCallbackEventReference
              ("'WizardCallback'", "'WizardCallback_Service1'", "'ClientCallback'", "null", "null", false);

            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            mWizardEngine = (Wizard.Asp.AspEngine)Application["WizardEngine"];
            mDataStoreContext = mWizardEngine.DataStore.GetNewContext();
            mWizardSession = mDataStoreContext.GetSession(this.Session.SessionID);
        }

        //The parameter eventArg is the argument parameter of 
        //the GetCallbackEventReference,passed from the JavaScript. 
        void ICallbackEventHandler.RaiseCallbackEvent(string eventArg)
        {
            String[] a = new String[31];
            a[1] = "Anna";
            a[2] = "Brittany";
            a[3] = "Cinderella";
            a[4] = "Diana";
            a[5] = "Eva";
            a[6] = "Fiona";
            a[7] = "Gunda";
            a[8] = "Hege";
            a[9] = "Inga";
            a[10] = "Johanna";
            a[11] = "Kitty";
            a[12] = "Linda";
            a[13] = "Nina";
            a[14] = "Ophelia";
            a[15] = "Petunia";
            a[16] = "Amanda";
            a[17] = "Raquel";
            a[18] = "Cindy";
            a[19] = "Doris";
            a[20] = "Eve";
            a[21] = "Evita";
            a[22] = "Sunniva";
            a[23] = "Tove";
            a[24] = "Unni";
            a[25] = "Violet";
            a[26] = "Liza";
            a[27] = "Elizabeth";
            a[28] = "Ellen";
            a[29] = "Wenche";
            a[30] = "Vicky";

            Int32 i;

            hint = string.Empty;
            if (eventArg.Length > 0)
            {
                for (i = 1; i < 31; i++)
                {
                    if (a[i].StartsWith(eventArg, StringComparison.OrdinalIgnoreCase))
                    {
                        if (hint.Length==0)
                            hint = a[i];
                        else
                            hint += ", " + a[i];
                    }
                }
            }
            if (hint.Length == 0) hint = "no suggestion";
        }

        //Return a string that will be received and processed  
        // by the clientCallback JavaScript funtion
        String ICallbackEventHandler.GetCallbackResult()
        {
            return hint;
        }


		protected void btnCompile_Click(object sender, EventArgs e)
		{
            try
            {
                //TextBox1.Text = DateTime.Now.ToLongTimeString();
                //Wizard.Compiler.Parsers.WizardParser parser = new Wizard.Compiler.Parsers.WizardParser(mWizardEngine);
                //Wizard.Source.IWizardApplication newApplication = null;
                //parser.CompileApplication(newApplication, mDataStore, this.tbSource.Text, Wizard.Compiler.Tokenizers.Language.Basic);
                //mDataStore.SaveChanges();
                //var oldApplication = mWizardSession.TempApplication;
                //mWizardSession.TempApplication = newApplication;
                //mDataStore.SaveChanges();
                //if (oldApplication != null)
                //{
                //    mDataStore.DeleteApplication(oldApplication);
                //    mDataStore.SaveChanges();
                //}
            }
            catch (Exception ex)
            {
                Label1.Text = "<PRE>" + HttpUtility.HtmlEncode(ex.ToString()) + "</PRE>";
            }
        }

        protected void btnClearDatabase_Click(object sender, EventArgs e)
        {
            mDataStoreContext.ClearDatabase();
        }

        protected void btnRun_Click(object sender, EventArgs e)
        {
            if (mWizardSession.TempApplication == null)
            {
                Label1.Text = "No Compiled Application Found";
            }
            else
            {
                Label1.Text = "Go go go";
            }
            
        }

        protected override void SavePageStateToPersistenceMedium(object viewState)
        {
            //empty
        }

        protected override object LoadPageStateFromPersistenceMedium()
        {
            return null;
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
        }

        protected override void LoadControlState(object savedState)
        {
            base.LoadControlState(savedState);
        }

        protected override object SaveControlState()
        {
            return base.SaveControlState();
        }

        protected override object SaveViewState()
        {
            return base.SaveViewState();
        }

        protected override StateBag ViewState
        {
            get
            {
                return base.ViewState;
            }
        }

        protected void btnRun_Click1(object sender, EventArgs e)
        {

        }
	}
}
